<!--#include file="../nav/admin_top.php" -->
<style type="text/css">
.sourcetb_out{
width:950px;
border:0px solid #CC3300;
overflow-x:scroll;
}
table.sourcetb{
border-collapse:collapse;
}
table.sourcetb th{
width:150px;
border: 1px solid #CCCCCC;
background-color: #E2F4C4;
padding: 5px;
text-align: center;
vertical-align: top;
}
table.sourcetb td{
border: 1px solid #CCCCCC;
background-color: #fcfcfc;
padding: 5px;
text-align: center;
vertical-align: top;
}
</style>
<div class="titleDiv">
	<div class="titleCaption">
		<span class="captionSpan">溯源管理 - 生产溯源录入</span>
	</div>
</div>
<div class="blockdiv">
	<div class="titlediv">配送批次信息</div>
	<table class="mytable" > 
		<tr>	
			<td class="mylabel"> 配送批次编号：</td>
			<td class="myinfo" style="color:#333333;font-size:14px;" >
				{$rs_source.code}
			</td>
			<td class="mylabel">配送日期：</td>
			<td class="myinfo" style="color:#FF3333;font-size:14px;" > 
				{$rs_source.senddate}
			</td>
			<td class="mylabel">溯源录入状态：</td>
			<td class="myinfo" style="color:#FF3333;font-size:14px;" > 
				<?PHP
				if($rs_source['pro_flag'] == '0'){
					echo '<div class="noflag">供应溯源</div>';
				}else{
					echo '<div class="yesflag">供应溯源</div>';
				}
				if($rs_source['make_flag'] == '0'){
					echo '<div class="noflag">生产溯源</div>';
				}else{
					echo '<div class="yesflag">生产溯源</div>';
				}
				if($rs_source['send_flag'] == '0'){
					echo '<div class="noflag">配送溯源</div>';
				}else{
					echo '<div class="yesflag">配送溯源</div>';
				}
				?>
			</td>			
		</tr>  
	</table> 
</div>
<div class="sourcetb_out" >
	<div style="width:<?PHP echo ( 2 + 6 + count($pro_fields) + 3 + count($make_fields) )*162+20; ?>px">
		<table class="sourcetb" id="sourcetb">
			<tr>
				<th>品类</th>
				<th>品名</th>
				 
				<th>
					入库时间
					<br><button class="autobtn" onclick="fnAllAuto('make_intime')">自动</button>
					<button class="autobtn" onclick="fnAllInput('make_intime','入库时间')">批量</button>
				</th>	
				<th>
					加工包装时间
					<br><button class="autobtn" onclick="fnAllAuto('make_time')">自动</button>
					<button class="autobtn" onclick="fnAllInput('make_time','加工包装时间')">批量</button>
				</th>	
				<th>
					加工质检员
					<br><button class="autobtn" onclick="fnAllAuto('make_testman')">自动</button>
					<button class="autobtn" onclick="fnAllInput('make_testman','加工质检员')">批量</button>
				</th>	
				<?PHP
				foreach($make_fields as $k=>$v){
					if(!is_array($v['option'])){
						$v['option'] = array();
					}
					$options = implode(',',$v['option']);
					echo '<th>'.$v['field_name'].'<br><button class="autobtn" onclick="fnAllInputField(\''.$v['id'].'\',\''.$v['field_name'].'\',\''.$v['field_type'].'\',\''.$v['field_length'].'\',\''.$v['field_decimal_len'].'\',\''.$options.'\')">批量</button></th>';
				}
				?>
				<th>
					基地
				</th>	
				<th>
					海拔（米）
				</th>	
				<th>
					采收时间
				</th>	
				<th>
					采收人员
				</th>
				<th>
					采收质检员
				</th>
				<th>
					空运时间
				</th>
				<?PHP
				foreach($pro_fields as $k=>$v){
					echo '<th>'.$v['field_name'].'</th>';
				}
				?>
			</tr>
			<?PHP
			foreach($rs_source_vege as $k=>$val){
				echo '<tr id="tr_'.$k.'" index="'.$k.'" class="tr_source" myid="'.$val['id'].'" >';
				echo '	<td >'.$val['vegetype_name'].'</td>';
				echo '	<td >'.$val['vege_name'].'</td>';
				echo '	<td >';
				echo '		<input type="text" class="textBox make_intime" id="make_intime_'.$k.'" maxlength="50" value="'.$val['make_intime'].'" />';
				echo '	</td>';
				echo '	<td >';
				echo '		<input type="text" class="textBox make_time" id="make_time_'.$k.'" maxlength="50" value="'.$val['make_time'].'" />';
				echo '	</td>';
				echo '	<td >';
				echo '		<input type="text" class="textBox make_testman" id="make_testman_'.$k.'" maxlength="50" value="'.$val['make_testman'].'" />';
				echo '	</td>';
				foreach($make_fields as $kk=>$vv){
					echo '<td >';
					switch($vv['field_type']){
						case "char":
							echo '<input type="text" class="textBox '.$vv['id'].'" id="'.$vv['id'].'_'.$k.'" field_name="'.$vv['field_name'].'"  field_type="'.$vv['field_type'].'"  field_length="'.$vv['field_length'].'"  field_decimal_len="'.$vv['field_decimal_len'].'" field_val="'.$val[$vv['id']].'" maxlength="'.$vv['field_length'].'" value="'.$val[$vv['id']].'" />';
							break;
						case "date":
							echo '<input type="text" class="textBox '.$vv['id'].'" id="'.$vv['id'].'_'.$k.'" field_name="'.$vv['field_name'].'"  field_type="'.$vv['field_type'].'"  field_length="'.$vv['field_length'].'"  field_decimal_len="'.$vv['field_decimal_len'].'" field_val="'.$val[$vv['id']].'" maxlength="19" value="'.$val[$vv['id']].'" />';
							break;
						case "int":
							echo '<input type="text" class="textBox '.$vv['id'].'" id="'.$vv['id'].'_'.$k.'" field_name="'.$vv['field_name'].'"  field_type="'.$vv['field_type'].'"  field_length="'.$vv['field_length'].'"  field_decimal_len="'.$vv['field_decimal_len'].'" field_val="'.$val[$vv['id']].'" maxlength="'.$vv['field_length'].'" value="'.$val[$vv['id']].'" />';
							break;
						case "float":
							echo '<input type="text" class="textBox '.$vv['id'].'" id="'.$vv['id'].'_'.$k.'" field_name="'.$vv['field_name'].'"  field_type="'.$vv['field_type'].'"  field_length="'.$vv['field_length'].'"  field_decimal_len="'.$vv['field_decimal_len'].'" field_val="'.$val[$vv['id']].'" maxlength="'.($vv['field_length']+$vv['field_decimal_len']+1).'" value="'.$val[$vv['id']].'" />';
							break;
						case "per":
							echo '<input type="text" style="width:60px;text-align:right" class="textBox '.$vv['id'].'" id="'.$vv['id'].'_'.$k.'" field_name="'.$vv['field_name'].'"  field_type="'.$vv['field_type'].'"  field_length="'.$vv['field_length'].'"  field_decimal_len="'.$vv['field_decimal_len'].'" field_val="'.$val[$vv['id']].'" maxlength="3" value="'.$val[$vv['id']].'" /> %';
							break;
						case "slt":
							echo '
							<select class="textBox '.$vv['id'].'" id="'.$vv['id'].'_'.$k.'" field_name="'.$vv['field_name'].'"  field_type="'.$vv['field_type'].'"  field_length="'.$vv['field_length'].'"  field_decimal_len="'.$vv['field_decimal_len'].'" field_val="'.$val[$vv['id']].'" >';
							foreach($vv['option'] as $options){
								if($options == $val[$vv['id']]){
									echo '<option value="'.$options.'" selected="selected">'.$options.'</option>';
								}else{
									echo '<option value="'.$options.'">'.$options.'</option>';
								}	
							}
							echo '
							</select>
							';
							break;
						case "mslt":
							echo '
							<select multiple="multiple" class="textBox '.$vv['id'].'" id="'.$vv['id'].'_'.$k.'" field_name="'.$vv['field_name'].'"  field_type="'.$vv['field_type'].'"  field_length="'.$vv['field_length'].'"  field_decimal_len="'.$vv['field_decimal_len'].'" field_val="'.$val[$vv['id']].'" >';
							foreach($vv['option'] as $options){
								if($options == $val[$vv['id']]){
									echo '<option value="'.$options.'" selected="selected">'.$options.'</option>';
								}else{
									echo '<option value="'.$options.'">'.$options.'</option>';
								}	
							}
							echo '
							</select>
							';
							break;
					}
					echo '</td>';
				}
				echo '	<td >'.$val['pro_base'].'</td>';
				echo '	<td >'.$val['pro_height'].'</td>';
				echo '	<td >'.$val['pro_picktime'].'</td>';
				echo '	<td >'.$val['pro_pickman'].'</td>';
				echo '	<td >'.$val['pro_testman'].'</td>';
				echo '	<td >'.$val['pro_plantime'].'</td>';
				foreach($pro_fields as $kk=>$vv){
					echo '<td >';
					echo $val[$vv['id']];
					echo '</td>';
				}
				echo '</tr>';
			}
			?>
		</table>
	</div>
</div>
<div  class="button_div">
	<button onclick="fnSave()">保 存</button>
	或 
	<button onclick="fnReturn()">返 回</button>
</div>
<script type="text/javascript">
var rs_source = {json_encode($rs_source)};
var rs_source_vege = {json_encode($rs_source_vege)};
var pro_fields = {json_encode($pro_fields)};
var make_fields = {json_encode($make_fields)};
var send_fields = {json_encode($send_fields)};
var url_save = '/admin_source_make/modify_save';
var url_return = '/admin_source_make/index';
$().ready(function(){
	showLoading();
	o2js.build('dates');
	o2js.build('ymprompt');
});
/* 自定义字段的批量输入 */
function fnAllInputField(field_id,field_name,field_type,field_length,field_decimal_len,option){
	ymPrompt.close();
	var $messge = '';
	if(field_type == 'slt'){
		$message = '<div style="margin:10px;">请输入批量值：<select id="allInput" class="textBox" field_id="'+field_id+'"  field_name="'+field_name+'"  field_type="'+field_type+'"  field_length="'+field_length+'"  field_decimal_len="'+field_decimal_len+'"  option="'+option+'" >';
		option = option.split(',');
		for(var i=0; i<option.length; i++){
			$message += '<option value="'+option[i]+'">'+option[i]+'</option>';
		}
		$message += '</select></div>';
	}else{
		$message = '<div style="margin:10px;">请输入批量值：<input type="text" class="textBox" field_id="'+field_id+'"  field_name="'+field_name+'"  field_type="'+field_type+'"  field_length="'+field_length+'"  field_decimal_len="'+field_decimal_len+'"  option="'+option+'" maxlength="50" id="allInput" onfocus="this.select()" /></div>';
	}
	ymPrompt.confirmInfo({
		icoCls:'',msgCls:'confirm',message:$message,title:'批量输入'+field_name,height:150,handler:fnAllInputFieldEnd,autoClose:false});	
	$('#allInput').focus();
}
function fnAllInputFieldEnd(tp){
	if(tp!='ok') return ymPrompt.close();
	var field_id = $('#allInput').attr('field_id');
	var val = $('#allInput').val();
	if(val == ''){
		alert('请输入批量值！');
		$('#allInput').focus();
	}else{
		$('.tr_source .'+field_id).val(val);
		ymPrompt.close();
	}
}
/* 批量输入 */
function fnAllInput(who,n){
	ymPrompt.close();
	ymPrompt.confirmInfo({
		icoCls:'',msgCls:'confirm',message:'<div style="margin:10px;">请输入批量值：<input type="text" class="textBox" who="'+who+'" maxlength="50" id="allInput" onfocus="this.select()" /></div>',title:'批量输入'+n,height:150,handler:fnAllInputEnd,autoClose:false});	
	$('#allInput').focus();
}
function fnAllInputEnd(tp){
	if(tp!='ok') return ymPrompt.close();
	var who = $('#allInput').attr('who');
	var val = $('#allInput').val();
	if(val == ''){
		alert('请输入批量值！');
		$('#allInput').focus();
	}else{
		$('.tr_source .'+who).val(val);
		ymPrompt.close();
	}	
}
/* 自动输入 */
function fnAllAuto(who){
		for(var i=0; i<rs_source_vege.length; i++){
			var obj = rs_source_vege[i];
			var myid = obj.id;
			switch(who){
				case "make_intime":
					if(obj.pro_plantime == ''){
						$('#make_intime_'+i).val('');
					}else{
						var tmp = strToDate(obj.pro_plantime);
						tmp = new Date(tmp.getTime() + (3600000*5));
						var fen = Math.floor( Math.random()*1000 )%59 ;
						var miao =  Math.floor( Math.random()*1000 )%59 ;
						if (fen<10) fen='0'+fen;
						if (miao<10) miao='0'+miao;
						$('#make_intime_'+i).val(tmp.eformat('yyyy-MM-dd hh:mm:ss'));
					}
					break;
				case "make_time":
					var tmpstr =$.trim( $('#make_intime_'+i).val() );
					if(tmpstr == ''){
						$('#make_time_'+i).val('');
					}else{
						var tmp = strToDate(tmpstr);
						var suijishu =  Math.floor( Math.random()*1000 )%61 ;
						tmp = new Date(tmp.getTime() + (60000*suijishu));
						
						//var fen = Math.floor( Math.random()*1000 )%59 ;
						//var miao =  Math.floor( Math.random()*1000 )%59 ;
						
						
						$('#make_time_'+i).val(tmp.eformat('yyyy-MM-dd hh:mm:ss'));
					}
					break;
				case "make_testman":
					$('#make_testman_'+i).val( obj.dft_make_testman );
					break;
			}
		}
}
function fnReturn(){
	jumpTo(url_return);
}

/* 保存 */
function fnSave(){
	var source_vege_obj = [];
	var field_obj = [];
	for(var i=0; i<rs_source_vege.length; i++){
		source_vege_obj.push({
			id:$.trim($('#tr_'+i).attr('myid')),
			make_intime:$.trim($('#make_intime_'+i).val()),
			make_time:$.trim($('#make_time_'+i).val()),
			make_testman:$.trim($('#make_testman_'+i).val())
		});
		for(var k=0; k<make_fields.length; k++){
			var tmpobj = {
				source_vege_id:$('#tr_'+i).attr('myid')
			};		
			tmpobj['field_id'] = make_fields[k].id;
			tmpobj['field_val'] = $.trim($('#'+make_fields[k].id+'_'+i).val());
			field_obj.push(tmpobj);
		}
	}	
  $('button').attr('disabled','disabled');
	$.ajax({
		async:true,
		type:'post',
		url:url_save,
		data:{
			source_id:rs_source.id,
			source_vege_obj:JSON.encode(source_vege_obj), 
			field_obj:JSON.encode(field_obj) 
		},
		success:function(e){
  		$('button').attr('disabled','');
			var ok = errorHandler(e);
			if(ok){　 
				jumpTo(url_return);
			}
		}
	});
}
</script>
<!--#include file="../nav/admin_bottom.php" -->